package com.shensi.adminCollect.controller.fleet;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.shensi.adminCollect.common.constant.UserConstants;
import com.shensi.common.core.domain.AjaxResult;
import com.shensi.adminCollect.common.utils.DateUtils;
import com.shensi.adminCollect.common.utils.PageInfoUtil;
import com.shensi.adminCollect.common.utils.StringUtils;
import com.shensi.adminCollect.domain.fleet.dto.AppletRechargeRecordDto;
import com.shensi.adminCollect.domain.fleet.vo.AppletRechargeRecordVo;
import com.shensi.adminCollect.service.fleet.AppletRechargeRecordService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

import com.shensi.common.core.domain.model.LoginUser;
import com.shensi.common.core.utils.SecurityUtils;
import com.shensi.common.core.domain.entity.SysUser;
@RestController
@RequestMapping("/applet")
@Slf4j
public class AppletRechargeRecordController {

    @Autowired
    private AppletRechargeRecordService appletRechargeRecordService;

    /**查询充值订单*/
    @PostMapping("/all/record")
    @ApiOperation(value = "查询所有充值订单", httpMethod = "POST", response = AppletRechargeRecordVo.class)
    public AjaxResult allRechargeRecord(@RequestBody AppletRechargeRecordDto appletRechargeRecordDto){
        if(Objects.isNull(appletRechargeRecordDto) || StringUtils.isEmpty(appletRechargeRecordDto.getBeginTime())
                || StringUtils.isEmpty(appletRechargeRecordDto.getEndTime())){
            return AjaxResult.error("请求参数异常");
        }

        long days = DateUtils.countDays(appletRechargeRecordDto.getBeginTime(), appletRechargeRecordDto.getEndTime());
        if(days >30){
            return AjaxResult.error("查询时间间隔不要超过30天");
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        // 未选择油站 取账号下油企油站
        if(UserConstants.LEVEL_TWO.equals(user.getUserLevel()) && Objects.isNull(appletRechargeRecordDto.getStationId())){
            appletRechargeRecordDto.setEnterpriseId(user.getEnterpriseId());
        }
        if(UserConstants.LEVEL_THREE.equals(user.getUserLevel()) && Objects.isNull(appletRechargeRecordDto.getStationId())){
            appletRechargeRecordDto.setStationId(user.getStationId());
        }

        log.info("allRechargeRecord请求参数：[{}]", JSONObject.toJSONString(appletRechargeRecordDto));

        Integer pageNum = appletRechargeRecordDto.getPageNum();
        Integer pageSize = appletRechargeRecordDto.getPageSize();
        List<AppletRechargeRecordVo> appletRechargeRecordVoList = appletRechargeRecordService.selectAppletRechargeRecord(appletRechargeRecordDto);
        PageInfo pageInfo = new PageInfo();
        if(CollectionUtils.isEmpty(appletRechargeRecordVoList)){
            pageInfo.setTotal(0);
            pageInfo.setPageSize(pageSize);
            pageInfo.setPageNum(pageNum);
            return AjaxResult.success(pageInfo);
        }
        pageInfo = PageInfoUtil.listPageInfo(pageNum, pageSize, appletRechargeRecordVoList);
        return AjaxResult.success(pageInfo);
    }


    /**查询充值订单统计*/
    @PostMapping("/all/record/statistics")
    @ApiOperation(value = "查询所有充值订单统计", httpMethod = "POST", response = AppletRechargeRecordVo.class)
    public AjaxResult allRechargeRecordStatistics(@RequestBody AppletRechargeRecordDto appletRechargeRecordDto){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();

        if(UserConstants.LEVEL_TWO.equals(user.getUserLevel()) && Objects.isNull(appletRechargeRecordDto.getStationId())){
            appletRechargeRecordDto.setEnterpriseId(user.getEnterpriseId());
        }
        if(UserConstants.LEVEL_THREE.equals(user.getUserLevel()) && Objects.isNull(appletRechargeRecordDto.getStationId())){
            appletRechargeRecordDto.setStationId(user.getStationId());
        }
        log.info("allRechargeRecordStatistics请求参数：[{}]", JSONObject.toJSONString(appletRechargeRecordDto));
        AppletRechargeRecordVo appletRechargeRecordVo = appletRechargeRecordService.selectAppletRechargeRecordStatistics(appletRechargeRecordDto);

        return AjaxResult.success(appletRechargeRecordVo);
    }

}
